---OOSE SoSe 2011 Aufgabe 3
--@author Mickey Reiss <mickeyreiss@gmail.com> (HU:417884)
--@author Oleg Mayevskiy <mayevskiy.oleg@googlemail.com> (233197)

require('classes')
require('tictactoe')
require('kalaha')
require('computerPlayer')

Class{
'GameCollection',
computerPlayer1 = ComputerPlayer,
computerPlayer2 = ComputerPlayer
}

function GameCollection:startSilent(gameName)
	local newgame
	if('TicTacToe' == gameName) then
		newgame = TicTacToe:create()
	elseif('Kalaha' == gameName) then
		newgame = Kalaha:create()
	else
		error('unknown game: '..gameName)
	end

	newgame:init()
	return newgame
end

function GameCollection:isHumanOpponent(opponent)
	if '1' == opponent then
		return true
	elseif '2' == opponent then
		return false
	else
		ierror('opponent must be 1 or 2')
	end
end

function GameCollection:startInteractive()

	local logger=Logger:create()
	logger.level = logger.WARNING



	print('Welcome! Please choose a game:')
	print('1	Kalaha')
	print('2	TicTacToe')
	print('Please type 1 or 2 to start a game:')
	local gameName = ''
	while('1'~=gameName and '2'~=gameName) do
		gameName = io.read()
		logger:fine('read',gameName)
		self:checkExit(gameName)
	end
	local game
	if('1' == gameName) then
		game = self:startSilent('Kalaha')
	elseif('2' == gameName) then
		game = self:startSilent('TicTacToe')
	end

	print('Choose an opponent!')
	print('1	human')
	print('2	computer')
	local opponent
	while('1'~=opponent and '2'~=opponent) do
		opponent = io.read()
		logger:fine('read',opponent)
		self:checkExit(opponent)
	end

  --Initialize deactivated computer players
	self.computerPlayer1 = ComputerPlayer:create(game.player1)
	self.computerPlayer1.game = game
	self.computerPlayer1:deactivate()

	self.computerPlayer2 = ComputerPlayer:create(game.player2)
	self.computerPlayer2.game = game
	self.computerPlayer2:deactivate()

	if(self:isHumanOpponent(opponent)) then
		print('Human vs. Human')
	else
		print('Now playing against the computer. Which player would you like to be?')
		print('1	1st player')
		print('2	2nd player')
		local humanPlayerNr
		while('1'~=humanPlayerNr and '2'~=humanPlayerNr) do
			humanPlayerNr = io.read()
			logger:fine('read',humanPlayerNr)
			self:checkExit(humanPlayerNr)
		end

		--initial set of computer opponent
		if('1' == humanPlayerNr) then
			self.computerPlayer2:activate()
		else
			self.computerPlayer1:activate()
		end
	end

	local isGameOver = false
  local nextPlayer

	while(not isGameOver) do
    local otherPlayer
    logger:info("Looking for other player (current player is "..game.currentPlayer.nr..")")
    if game.currentPlayer.nr == 1 then
      otherPlayer = self.computerPlayer2
    else
      otherPlayer = self.computerPlayer1
    end

		game:printPossibleTurns()
		game:print()
		print('Current Player: ',game.currentPlayer.name)
		local input = game.currentPlayer:read()
		logger:fine('read',input)
		logger:finer('type input is',type(input))
		self:checkExit(input)

		if('restart' == input) then
			game:restart()
		elseif('on' == input) then
      otherPlayer:activate()
			print(otherPlayer.name..' is now controlled by the computer.')
		elseif('off' == input) then
      otherPlayer:deactivate()
			print(otherPlayer.name..' is now controlled by a human.')
		elseif(tonumber(input) and 1<=tonumber(input) and tonumber(input)<=9) then

			nextPlayer, isGameOver = game:move(tonumber(input))
		else
			print("/-----------------------------------\\" )
			print("| Invalid input. Allowed values are: |" )
			print("| 0                     exit program |")
			print("| 1-9                    make a move |")
			print("| restart               restart game |")
			print("| on             turn pc opponent on |")
			print("| off           turn pc opponent off |")
			print("\\------------------------------------/" )
		end
	end

	--print game over
	
	local winner = nextPlayer
	
	print('Game Over!')
	if(not isNil(winner)) then
		print('Player '..winner..' wins')
	else
		print('a draw')
	end
	
	game:print()
end

function GameCollection:checkExit(input)
	if('0' == input or 0 == input) then
		print('Thanks for playing--Goodbye! :)')
		os.exit()
	end
end
